package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.mjdb.data.field.NullField;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/

public abstract class DataAction extends NullField implements ITag {

	public static String DEFAULT_ID = "_Action";
	DataState start = new DataState(false);
	DataState running = new DataState(false);
	DataState end = new DataState(false);
	DataSize steps = new DataSize(TagModifier.SIZE_TYPE_COUNT,10);
	DataSize currentStep = new DataSize(TagModifier.SIZE_TYPE_COUNT,0);
	DataState looped = new DataState(false);
	DataState pingpong = new DataState(false);
	DataState startTrigger = new DataState(false);
	DataState stopTrigger = new DataState(false);
	DataString direction = new DataString(ProcessConstant.FORWARD);

	DataNode parent;

public DataAction(DataNode parent) {
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch

	this.parent = parent;
}

public boolean buildFromElement(XmlElement element)
{
		if(element == null)
			return false;

		String eleName = element.getName();

		if(eleName.equals(Token.START))
		{
			start = new DataState(false);
			start.buildFromgrfxML(element);
		}

		else if(eleName.equals(Token.RUNNING))
		{
	  		running = new DataState(false);
	  		running.buildFromgrfxML(element);
		}

		else if(eleName.equals(Token.END))
		{
	  		end = new DataState(false);
	  		end.buildFromgrfxML(element);
		}

		else if(eleName.equals(Token.STEPS))
		{
			steps = new DataSize(TagModifier.SIZE_TYPE_COUNT,10);
			steps.buildFromgrfxML(element,TagModifier.SIZE_TYPE_COUNT);
		}

		else if(eleName.equals(Token.CURRENT_STEP))
		{
			currentStep = new DataSize(TagModifier.SIZE_TYPE_COUNT);
	  		currentStep.buildFromgrfxML(element,TagModifier.SIZE_TYPE_COUNT);
		}

		else if(eleName.equals(Token.LOOPED))
		{
			looped = new DataState(false);
			looped.buildFromgrfxML(element);
		}

		else if(eleName.equals(Token.PINGPONG))
		{
			pingpong = new DataState(false);
			pingpong.buildFromgrfxML(element);
		}

		else if(eleName.equals(Token.START_TRIGGER))
		{
			startTrigger = new DataState(false);
			startTrigger.buildFromgrfxML(element);
		}

		else if(eleName.equals(Token.STOP_TRIGGER))
		{
			stopTrigger = new DataState(false);
			stopTrigger.buildFromgrfxML(element);
		}

		else if(eleName.equals(Token.DIRECTION))
		{
			direction = new DataString(ProcessConstant.FORWARD);
			direction.buildFromgrfxML(element);
		}
		else
			return false;
	return true;
}
public boolean copyFromElement(XmlElement element)
{
		if(element == null)
			return false;

		String eleName = element.getName();

		if(eleName.equals(Token.START))
		{
			start.copyFromgrfxML(element);
		}

		else if(eleName.equals(Token.RUNNING))
		{
	  		running.copyFromgrfxML(element);
		}

		else if(eleName.equals(Token.END))
		{
	  		end.copyFromgrfxML(element);
		}

		else if(eleName.equals(Token.STEPS))
		{
			steps.copyFromgrfxML(element,TagModifier.SIZE_TYPE_COUNT);
		}

		else if(eleName.equals(Token.CURRENT_STEP))
		{
			currentStep.copyFromgrfxML(element,TagModifier.SIZE_TYPE_COUNT);
		}

		else if(eleName.equals(Token.LOOPED))
		{
			looped.copyFromgrfxML(element);
		}

		else if(eleName.equals(Token.PINGPONG))
		{
			pingpong.copyFromgrfxML(element);
		}

		else if(eleName.equals(Token.START_TRIGGER))
		{
			startTrigger.copyFromgrfxML(element);
		}

		else if(eleName.equals(Token.STOP_TRIGGER))
		{
			stopTrigger.copyFromgrfxML(element);
		}

		else if(eleName.equals(Token.DIRECTION))
		{
			direction.copyFromgrfxML(element);
		}
		else
			return false;
	return true;
}
public abstract DataAction getClone();
public abstract DataAction getClone(String path);
public void getClonedElements(String path,DataAction newAction)
{
	newAction.start = start.getClone(path);
	newAction.running = running.getClone(path);
	newAction.end = end.getClone(path);
	newAction.steps = steps.getClone(path);
	newAction.looped = looped.getClone(path);
	newAction.pingpong = pingpong.getClone(path);
	newAction.startTrigger = startTrigger.getClone(path);
	newAction.stopTrigger = stopTrigger.getClone(path);
	newAction.direction = direction.getClone(path);
}
public void getClonedElements(DataAction newAction)
{
	newAction.start = start.getClone();
	newAction.running = running.getClone();
	newAction.end = end.getClone();
	newAction.steps = steps.getClone();
	newAction.looped = looped.getClone();
	newAction.pingpong = pingpong.getClone();
	newAction.startTrigger = startTrigger.getClone();
	newAction.stopTrigger = stopTrigger.getClone();
	newAction.direction = direction.getClone();
}
public DataSize getCurrentStep() {
	return currentStep;
}
public DataString getDirection() {
	return direction;
}
public DataState getEnd() {
	return end;
}
public DataState getLooped() {
	return looped;
}
public DataState getPingpong() {
	return pingpong;
}
public DataState getRunning() {
	return running;
}
public DataState getStart() {
	return start;
}
public DataState getStartTrigger() {
	return startTrigger;
}
public DataSize getSteps() {
	return steps;
}
public DataState getStopTrigger() {
	return stopTrigger;
}

public DataNode getParent()
{
	return parent;
}

protected abstract void process();
public void register(DataLinker store) {

	try{
		store.add(start,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(running,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(end,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(steps,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(currentStep,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(looped,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(pingpong,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(startTrigger,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(stopTrigger,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(direction,this);
	}
	catch(LockException le)
	{}//Dummy catch
}
public void replace(Field oldObj, Field newObj) {

	if(oldObj==parent)
	{
		parent = (DataNode)newObj;
	}

	if(start==oldObj)
		start=(DataState)newObj;

	if(running==oldObj)
		running=(DataState)newObj;

	if(end==oldObj)
		end=(DataState)newObj;

	if(steps==oldObj)
		steps=(DataSize)newObj;

	if(currentStep==oldObj)
		currentStep=(DataSize)newObj;

	if(looped==oldObj)
		looped=(DataState)newObj;

	if(pingpong==oldObj)
		pingpong=(DataState)newObj;

	if(startTrigger==oldObj)
		startTrigger=(DataState)newObj;

	if(stopTrigger==oldObj)
		stopTrigger=(DataState)newObj;

	if(direction==oldObj)
		direction=(DataString)newObj;
}
public void resetChanged() {
	super.resetChanged();
	start.resetChanged();
	running.resetChanged();
	end.resetChanged();
	steps.resetChanged();
	currentStep.resetChanged();
	looped.resetChanged();
	pingpong.resetChanged();
	startTrigger.resetChanged();
	stopTrigger.resetChanged();
	direction.resetChanged();
}
protected void setCurrentStep(DataSize newCurrentStep) {
	currentStep = newCurrentStep;
}
protected void setDirection(DataString newDirection) {
	direction = newDirection;
}
protected void setEnd(DataState newEnd) {
	end = newEnd;
}
protected void setLooped(DataState newLooped) {
	looped = newLooped;
}
protected void setPingpong(DataState newPingpong) {
	pingpong = newPingpong;
}
protected void setRunning(DataState newRunning) {
	running = newRunning;
}
protected void setStart(DataState newStart) {
	start = newStart;
}
protected void setStartTrigger(DataState newStartTrigger) {
	startTrigger = newStartTrigger;
}
protected void setSteps(DataSize newSteps) {
	steps = newSteps;
}
protected void setStopTrigger(DataState newStopTrigger) {
	stopTrigger = newStopTrigger;
}
public String togrfxML(String name) {
	String xml = start.togrfxML(Token.START);

	xml += running.togrfxML("running");
	xml += end.togrfxML(Token.END);
	xml += steps.togrfxML(new DataSize(10),"steps");
	xml += currentStep.togrfxML("currentStep");
	xml += looped.togrfxML("looped");
	xml += pingpong.togrfxML("pingpong");
	xml += startTrigger.togrfxML("startTrigger");
	xml += stopTrigger.togrfxML("stopTrigger");
	xml += direction.togrfxML(new DataString(ProcessConstant.FORWARD),"direction");

	return xml;
}
public String togrfxML(String name,TagModifier modifier) {
	return togrfxML(name);
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
}
